/****************************************************
	文件：GameRoot.cs
	作者：HuskyT
	邮箱：1005240602@qq.com
	日期：2020/10/11 19:13:44
	功能：三消原型（Game 根节点）
*****************************************************/

using System;
using UnityEngine;
using HTUtility;
using HTFramework;
using HTFramework.ResKit;
using HTFramework.UIKit;
using UnityEngine.EventSystems;

namespace HTDemo01
{
    public class GameRoot : MonoBehaviour
    {
        /// <summary>
        /// 游戏根节点
        /// </summary>
        public Transform GameRootTrans { get; private set; }
        /// <summary>
        /// UI根节点
        /// </summary>
        public RectTransform UIRootTrans { get; private set; }
        /// <summary>
        /// UI窗口节点
        /// </summary>
        public RectTransform WndRootTrans { get; private set; }
        /// <summary>
        /// UGUI 的事件系统
        /// </summary>
        public EventSystem EventSys { get; private set; }
        /// <summary>
        /// UI摄像机
        /// </summary>
        public Camera UICamera { get; private set; }
        /// <summary>
        /// 主摄像机
        /// </summary>
        public Camera MainCamera { get; private set; }
        /// <summary>
        /// 场景中的回收节点
        /// </summary>
        public Transform RecycleTrans { get; private set; }
        /// <summary>
        /// 场景节点
        /// </summary>
        public Transform SceneTrans { get; private set; }

        private AudioClip mClip;
        private AudioSource Audio;

        private void Awake()
        {
            DontDestroyOnLoad(this);
            GameRootTrans = transform;
            UIRootTrans = transform.Find(GConsts.UI_ROOT_FIND_PATH).GetComponent<RectTransform>();
            WndRootTrans = transform.Find(GConsts.WND_ROOT_FIND_PATH).GetComponent<RectTransform>();
            EventSys = transform.Find(GConsts.EVENT_SYSTEM_FIND_PATH).GetComponent<EventSystem>();
            UICamera = transform.Find(GConsts.UI_CAMERA_FIND_PATH).GetComponent<Camera>();
            MainCamera = transform.Find(GConsts.MAIN_CAMERA_FIND_PATH).GetComponent<Camera>();
            RecycleTrans = transform.Find(GConsts.RECYCLE_FIND_PATH);
            SceneTrans = transform.Find(GConsts.SCENE_FIND_PATH);

            Audio = GetComponent<AudioSource>();
        }
        private void Start()
        {
            Init();
            SceneMgr.Instance.LoadScene(GConsts.LOGIN_SCENE); //加载登录场景
        }
        private void Update()
        {
            UIMgr.Instance.OnUpdate();
            if (Input.GetKeyDown(KeyCode.Q))
            {
                Resources.UnloadUnusedAssets();
                HTLogger.Warning("编辑器下清空无用资源");
            }
        }
        private void OnApplicationQuit()
        {
#if UNITY_EDITOR
            Resources.UnloadUnusedAssets();
#endif
        }

        private void Init()
        {
            HTLoggerInstaller.Instance.Install();

            ResMgr.Instance.Init(); //资源加载
            ResLoader.Instance.Init();
            GOMgr.Instance.Init(RecycleTrans); //GameObject 实例化
            GOLoader.Instance.Init(RecycleTrans, SceneTrans);
            SchemeMgr.Instance.Init(); //配置表
            UIMgr.Instance.Init(UIRootTrans, WndRootTrans, EventSys, UICamera); //UI
            SceneMgr.Instance.Init(this); //场景管理

            LevelMgr.Instance.Init(MainCamera); //关卡管理

            RegisterUI();
        }
        private void RegisterUI()
        {
            UIMgr.Instance.Register<UILoading>(GConsts.LOADING_PANEL);
            UIMgr.Instance.Register<UILogin>(GConsts.LOGIN_PANEL);
            UIMgr.Instance.Register<UILevel>(GConsts.LEVEL_PANEL);
        }
    }
}
